<?php

Yii::import('application.vendors.*');
require_once('Zend/Loader.php');

class CouponController extends RController
{

    public function init()
    {
        parent::init();
        $baseUrl = Yii::app()->baseUrl;
        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile($baseUrl . '/js/jquery-ui-1.10.2.custom.min.js');
        $cs->registerCssFile($baseUrl . '/css/ui-lightness/jquery-ui-1.10.2.custom.min.css');
    }

    public function actionIndex()
    {
        $criteria = new CDbCriteria;
        $dataProvider = new CActiveDataProvider('Coupon', array(
                    'criteria' => $criteria
                ));

        $this->render('index', array(
            'dataProvider' => $dataProvider
        ));
    }

    public function actionCreateBarCode()
    {
        Zend_Loader::loadClass('Zend_Barcode');
        Zend_Loader::loadClass('Zend_Barcode_Object_Code39');
        Zend_Loader::loadClass('Zend_Barcode_Renderer_Image');
        Zend_Loader::loadClass('Zend_Validate_Barcode_Code39');
        Zend_Loader::loadClass('Zend_Barcode_Renderer_Pdf');
        Zend_Loader::loadClass('Zend_Pdf');
        Zend_Loader::loadClass('Zend_Pdf_Page');

        $pdf = new Zend_Pdf();
        $pdf->properties['Title'] = "Title";
        $pdf->properties['Author'] = "Author";
        $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
        // Need to set a TTF.
        Zend_Barcode::setBarcodeFont(Yii::app()->basePath . '/font/OpenSans-Regular.ttf');
        $topOffset = 50;
        $leftOffset = 50;
        $page = 0;

        $coupons = Coupon::model()->findAll();

        $count = 0;
        foreach ($coupons as $key => $product) {
            $count++;
            if ($topOffset >= 840) {
                // If it's the end of the page we add a new page
                $topOffset = 50;
                $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
                $page++;
            }
            $barcodeOptions = array('text' => $product->code);

            $rendererOptions = array(
                'topOffset' => $topOffset,
                'leftOffset' => $leftOffset
            );

            $pdfWithBarcode = Zend_Barcode::factory('code39', 'pdf', $barcodeOptions, $rendererOptions)->setResource($pdf, $page)->draw();
            // 100 is the space between each barcode
            if ($count == 4) {
                $topOffset = $topOffset + 50;
                $leftOffset = -70;
                $count = 0;
            }

            $leftOffset = $leftOffset + 120;
        }
        $pdfWithBarcode->render();
        $pdfFilename = 'Barcode.pdf';
        $pdfWithBarcode->save($pdfFilename);
        header('Content-disposition: attachment; filename=' . $pdfFilename);
        header('Content-type: application/pdf');
        readfile($pdfFilename);
        exit(0);
    }

    public function actionCreate()
    {
        $model = new CouponForm;

        if (isset($_POST['CouponForm'])) {
            $model->setAttributes($_POST['CouponForm']);
            Coupon::model()->generateCoupon($model->amount, $model->value, $model->from_date, $model->to_date);
        }

        $this->render('_form', array(
            'model' => $model
        ));
    }

    public function filters()
    {
        return array(
            'rights'
        );
    }

}